﻿using GalaSoft.MvvmLight;
using System.Collections.ObjectModel;
using SmartMusicBox.Model;
using System.Windows.Media.Imaging;
using System;
using SmartMusicBox.Logic;
using SmartMusicBox.Framework.Helper;
using System.Diagnostics;
using SmartMusicBox.Framework;
using SmartMusicBox.ServiceModel;
using SmartMusicBox.Framework.Navigation;
using GalaSoft.MvvmLight.Command;
using System.Windows;

namespace SmartMusicBox.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class HomePageViewModel : SmartViewModelBase
    {
        #region Property
        public ObservableCollection<TileModel> Tiles { get; set; }

        private RelayCommand _OnGoPlayerTapCommand;
        public RelayCommand OnGoPlayerTapCommand
        {
            get { return _OnGoPlayerTapCommand; }
            set
            {
                _OnGoPlayerTapCommand = value;
                this.RaisePropertyChanged("OnGoPlayerTapCommand");
            }
        }

        private Visibility _ShowGoPlayerImage = Visibility.Collapsed;
        public Visibility ShowGoPlayerImage
        {
            get { return this._ShowGoPlayerImage; }
            set
            {
                this._ShowGoPlayerImage = value;
                this.RaisePropertyChanged("ShowGoPlayerImage");
            }
        }
        #endregion

        #region Construct
        /// <summary>
        /// Initializes a new instance of the HomePageViewModel class.
        /// </summary>
        public HomePageViewModel()
        {
            Init();
        }
        #endregion

        #region Method
        private void Init()
        {
            BindCommand();
            SetPageResource();
            LoadTiles();
            UpdateTiles();
        }

        private void SetPageResource()
        {
            ShowGoPlayerImage = Visibility.Collapsed;
        }

        private void BindCommand()
        {
            OnGoPlayerTapCommand = new RelayCommand(() => {
                MessengerInstance.Send(new NavigateMessage("PlayerPage", null));
            });
        }

        private void LoadTiles()
        {
            var channels = ChannelLogic.GetPinnedChannel();
            Tiles = TileLogic.GetTiles(channels);
        }

        private void UpdateTiles()
        {
            foreach (var t in Tiles)
            {
                var tile = t;
                var channelNo = tile.ChannelNo;
                SongLogic.SendRequestForGetSongs(channelNo,
                    (songs) =>
                    {
                        DispatcherHelper.BeginInvokeOnUIThread(() =>
                            {
                                if (songs == null || songs.Length == 0)
                                {
                                    tile.SongInfo = "暂无歌曲";
                                }
                                else
                                {
                                    var song = songs[0];
                                    tile.SongInfo = SongLogic.GetSongInfo(song);
                                    tile.Picture = SongLogic.GetImageSourceByUrl(song.Picture);
                                    tile.SongData = song;

                                    tile.OnTapCommand = new RelayCommand<TileModel>((data) => { this.ExcuteTap(tile); });
                                }
                            });
                    },
                    (errmsg) =>
                    {
                        DispatcherHelper.BeginInvokeOnUIThread(() =>
                        {
                            Debug.WriteLine(errmsg);
                            tile.SongInfo = "暂无歌曲";
                        });
                    });
            }
        }

        private void ExcuteTap(TileModel tile)
        {
            if (tile == null || tile.SongData == null) return;

            var song = tile.SongData as SongModel;
            var transferData = new SongPlayerModel()
            {
                SongInfo = tile.SongInfo,
                ChannelNo = tile.ChannelNo,
                ChannelName = tile.ChannelName,
                Picture = SongLogic.GetImageSourceByUrl(song.Picture.Replace("mpic", "lpic")),
                URL = song.Url,
                Title = song.Title
            };

            MessengerInstance.Send(new NavigateMessage("PlayerPage", transferData));
        }

        public override void NavigateTo(object o)
        {
            ShowGoPlayerImage = Visibility.Visible;
            UpdateTiles();
        }
        #endregion
    }
}